package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import org.springframework.web.multipart.MultipartFile;
import pojo.User;
import service.IMainService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@Controller
@RequestMapping("/main")
public class MainController {
    @Resource
    private IMainService mainService;

    /**
     * 登录
     *
     * @param username 用户名
     * @param password 密码
     * @param type 用户类型
     * @return 登录结果, 用户对象
     */
    @RequestMapping("/login")
    @ResponseBody
    public Map<String, Object> login(
            @RequestParam String username,
            @RequestParam String password,
            @RequestParam String type
    ) {

        Byte typeb = Byte.valueOf(type);

        User userp = new User();
        userp.setUsername(username);
        userp.setPassword(password);
        userp.setType(typeb);


        User user = mainService.login(userp);

        Map<String, Object> loginMsg = new HashMap<>();

        if (user == null) {
            // 登录失败
            loginMsg.put("success", false);

        } else {
            // 登录成功
            loginMsg.put("success", true);
            loginMsg.put("user", user);

        }
        return loginMsg;

    }

    /**
     * 注册
     *
     * @param user 用户参数
     * @return 注册结果
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> register(@RequestBody User user) {
        int result = mainService.register(user);
        Map<String, Object> registerMsg = new HashMap<>();
        if (result > 0) {
            // 注册成功
            registerMsg.put("success", true);
        } else {
            // 注册失败
            registerMsg.put("success", false);
        }
        return registerMsg;
    }


    @RequestMapping(value = "/addImg")
    @ResponseBody
    public String addImg(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request){
        //判断所上传文件是否存在
        if(multipartFile.isEmpty()){
            return "上传错误";
        }
        //获取上传文件的原始名称
        String originalFilename = multipartFile.getOriginalFilename();
        //设置上传文件的保存地址目录
        String dirPath =  "E:/javaEE学习项目/graduation/graduationApi/src/main/webapp/upload/";
        System.out.println(request.getSession());
        File filePath = new File(dirPath);
        //如果保存的地址不存在,就先创建目录
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        //获取当前文件的后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);

        //使用UUID重新命名上传的文件名称(UUID(唯一识别码)+原始文件名称后缀)
       String newFileName = UUID.randomUUID() + "." + suffix;
        try {
            //创建可能会有错误,强制添加异常
            multipartFile.transferTo(new File(dirPath + newFileName));
        } catch (IOException e) {
            e.printStackTrace();
            return "上传错误";
        }
        System.out.println(dirPath + newFileName);
        return "upload/" + newFileName;


    }

}
